apiready = function() {
    j = 0;
    var arr = new Array;
    var arr1 = new Array;
    var body = {
        code: 'chaoliufushi',
        token: $api.getStorage('token'),
        j: j
    }
    window.openGoods = function(i) {
        api.openFrame({
            url: '../formwork.html',
            rect: {
                x: 0,
                y: 0,
                w: 'auto',
                h: 'auto'
            },
            pageParam: {
                url: arr[i],
                id: arr1[i],
                token: token
            },
            allowEdit:true
        });
    }

    if (length != '') {
        $api.byId('refresh').style = "display:block";
        api.addEventListener({
            name: 'scrolltobottom'
        }, function(ret, err) {

            var body = {
                code: 'chaoliufushi',
                token: $api.getStorage('token'),
                j: j++
            }
            api.ajax({
                url: 'http://www.mk517.com/webApi/goodslist.php',
                method: 'post',
                dataType: 'json',
                "headers": {
                    "Content-Type": "application/json"
                },
                data: {
                    body: body
                }
            }, function(ret, err) {
                showGoods();
            });

        });
    } else {
        $api.byId('refresh').style = "display:none";

    }

    showGoods();

    function showGoods() {
        api.ajax({
            url: 'http://www.mk517.com/webApi/goodslist.php',
            method: 'post',
            dataType: 'json',
            "headers": {
                "Content-Type": "application/json"
            },
            data: {
                body: body
            }
        }, function(ret, err) {
            if (ret) {
                length = ret.length;
                $api.byId('goodsList').innerHTML = '';
                for (var i = 0; i < ret.length; i++) {
                    data = ret[i];
                    arr.push(data.url);
                    arr1.push(data.id);
                    token = data.token;
                    $api.byId('goodsList').innerHTML +=
                        '<ul class="aui-list aui-media-list" onclick="openGoods(' + i + ');">' +
                        '<li class="aui-list-item">' +
                        '<div class="aui-media-list-item-inner">' +
                        '<div class="aui-list-item-media">' +
                        '<img src="' + data.img + '">' +
                        '</div>' +
                        '<div class="aui-list-item-inner">' +
                        '<div class="aui-list-item-text">' +
                        '<div class="aui-list-item-title">' + data.title + '</div>' +
                        '</div>' +
                        '<div class="aui-list-item-text">' +
                        '<div>已售：' + data.sell + '件&nbsp;&nbsp;&nbsp;&nbsp;<span class="aui-bg-pink" style="color:#fff;font-size:14px;">返利</span></div>' +
                        '</div>' +
                        '<div class="aui-list-item-text">' +
                        '<div><span style="color:red;font-size:20px;">￥' + data.discount_price + '</span>&nbsp; <span><s>￥' + data.price + '</s></span></div>' +
                        '</div>' +
                        '<div class="aui-list-item-text aui-pull-right">' +
                        '<a class="aui-info-item aui-btn aui-btn-warning "  onclick="openGoods(' + i + ');">去抢购</a></div></div></div></li></ul>';
                }
                // alert(JSON.stringify(ret));
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }


}
